package com.ruoyi.mongo.word.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mongo.service.MongoCommonService;
import com.ruoyi.mongo.service.MongoConstants;
import com.ruoyi.mongo.word.service.WordListInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class WordListService implements WordListInterface {

    @Autowired
    private MongoCommonService mongoCommonService;

    /**
     * 得到通用的-Query
     * 练习标识
     * */
    @Override
    public Query getRecordDetailQuery(String status, String practice){
        Query query = new Query();
        if (status != null && !status.isEmpty()){
            Criteria criteria = Criteria.where(MongoConstants.MONGO_STATUS).is(status);
            query.addCriteria(criteria);
        }

        if (practice != null && !practice.isEmpty()){
            Criteria criteria = Criteria.where(MongoConstants.MONGO_PRACTICE).is(practice);
            query.addCriteria(criteria);
        }
        return query;
    }

    @Override
    public Query getRecordDetailQuery(String practice){
        Query query = new Query();

        if (practice != null && !practice.isEmpty()){
            Criteria criteria = Criteria.where(MongoConstants.MONGO_PRACTICE).is(practice);
            query.addCriteria(criteria);
        }
        return query;
    }
    @Override
    public <T> void insertStudyData(T entity, String collectionName) {
        //根据登录用户创建学习记录
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String name = collectionName + "_" + userId;
        T inserted = mongoCommonService.insertOneAndReturn(entity, name);
        log.info("集合:{},插入,时间:{},value:{}", name, DateUtils.getTime(), inserted);
    }
}
